<?php

include 'DBconn.php';

class CustomerMaster {

    public function generateClientCustomerCode() {
        $res = "";
        $query = "select customer_code from client_customer_code order by serialnumber desc limit 1";
        $result = mysql_query($query) or die(mysql_error());
        if (mysql_num_rows($result)) {
            $row = mysql_fetch_array($result);

            $str = explode("CT", $row[0]);
            $num = intval($str[1]);
            $res = "CT" . ++$num;
        } else {
            $res = 'CT1001';
        }
        return $res;
    }

    public function generateClientSupplierCode() {
        $res = "";
        $query = "select supplier_code from client_supplier_code order by serialnumber desc limit 1";
        $result = mysql_query($query) or die(mysql_error());
        if (mysql_num_rows($result)) {
            $row = mysql_fetch_array($result);

            $str = explode("SP", $row[0]);
            $num = intval($str[1]);
            $res = "SP" . ++$num;
        } else {
            $res = 'SP1001';
        }
        return $res;
    }

    public function generateClientBothCode() {
        $res = "";
        $query = "select both_code from client_both_code order by serialnumber desc limit 1";
        $result = mysql_query($query) or die(mysql_error());
        if (mysql_num_rows($result)) {
            $row = mysql_fetch_array($result);

            $str = explode("BT", $row[0]);
            $num = intval($str[1]);
            $res = "BT" . ++$num;
        } else {
            $res = 'BT1001';
        }
        return $res;
    }

    public function generateClientUncategorizedCode() {
        $res = "";
        $query = "select uncategorized_code from client_uncategorized_code order by serialnumber desc limit 1";
        $result = mysql_query($query) or die(mysql_error());
        if (mysql_num_rows($result)) {
            $row = mysql_fetch_array($result);

            $str = explode("UN", $row[0]);
            $num = intval($str[1]);
            $res = "UN" . ++$num;
        } else {
            $res = 'UN1001';
        }
        return $res;
    }

    public function addNewClient($VObject) {
        $client_id = "";

        if ($VObject->cust_type == "CUSTOMER") {
            $client_id = $this->generateClientCustomerCode();
            mysql_query("insert into client_customer_code(customer_code) values ('$client_id')") or die(mysql_error());
        } else if ($VObject->cust_type == "SUPPLIER") {
            $client_id = $this->generateClientSupplierCode();
            mysql_query("insert into client_supplier_code(supplier_code) values ('$client_id')") or die(mysql_error());
        } else if ($VObject->cust_type == "BOTH") {
            $client_id = $this->generateClientBothCode();
            mysql_query("insert into client_both_code(both_code) values ('$client_id')") or die(mysql_error());
        } else if ($VObject->cust_type == "UN-CATEGORIZED") {
            $client_id = $this->generateClientUncategorizedCode();
            mysql_query("insert into client_uncategorized_code(uncategorized_code) values ('$client_id')") or die(mysql_error());
        }

        $query = "insert into client_header(client_id,client_name,cust_type,country_serialnumber_fk,state_serialnumber_fk,client_off_addr,off_phone,client_del_addr,del_phone,client_fax,client_email,client_url,client_contact_person,client_contact_phone,client_contact_email,client_remarks,client_regno1,client_regno2,client_pricecategory,cre_userid,cre_datetime,activestatus) values('$client_id','$VObject->client_name','$VObject->cust_type','$VObject->country_serialnumber_fk','$VObject->state_serialnumber_fk','$VObject->client_off_addr','$VObject->off_phone','$VObject->client_del_addr','$VObject->del_phone','$VObject->client_fax','$VObject->client_email','$VObject->client_url','$VObject->client_contact_person','$VObject->client_contact_phone','$VObject->client_contact_email','$VObject->client_remarks','$VObject->client_regno1','$VObject->client_regno2','$VObject->client_pricecategory','$VObject->cre_userid','$VObject->cre_datetime',1)";
        $s = mysql_query($query) or die(mysql_error());
        if ($s == 1) {
            mysql_query("COMMIT");
        } else {
            mysql_query("ROLLBACK");
        }
        return $this->getAllClients();
    }

    public function getAllSuppliers() {
        $result = mysql_query("select * from client_header where activestatus=1 and (cust_type='SUPPLIER' or cust_type='BOTH')") or die(mysql_error());
        $ret = array();
        while ($row = mysql_fetch_object($result)) {
            $tmp = new VoFilteredClient;
            $tmp->serialnumber = (int) $row->serialnumber;
            $tmp->client_name = $row->client_name;
            $tmp->client_email = $row->client_email;
            $tmp->client_contact_person = $row->client_contact_person;
            $tmp->client_contact_phone = $row->client_contact_phone;
			$tmp->client_contact_email = $row->client_contact_email;
            $tmp->isSelected = false;
            $tmp->hilight = 0;
            $ret[] = $tmp;
        }
        mysql_free_result($result);
        return $ret;
    }

    public function getAllCustomer() {
        $result = mysql_query("select * from client_header where activestatus=1 and (cust_type='CUSTOMER' or cust_type='BOTH')") or die(mysql_error());
        $ret = array();
        while ($row = mysql_fetch_object($result)) {
            $tmp = new VoFilteredClient;
            $tmp->serialnumber = (int) $row->serialnumber;
            $tmp->client_name = $row->client_name;
            $tmp->client_email = $row->client_email;
            $tmp->client_contact_email = $row->client_contact_email;
            $tmp->isSelected = false;
			$tmp->hilight = 0;
            $ret[] = $tmp;
        }
        mysql_free_result($result);
        return $ret;
    }

    public function getAllClients() {
        $result = mysql_query("Select ch.activestatus,ch.cre_datetime,ch.cre_userid,ch.client_pricecategory,ch.client_regno2,ch.client_regno1,ch.client_remarks,ch.client_contact_email,ch.client_contact_phone,ch.client_contact_person,
		ch.client_url,ch.client_email,ch.client_fax,ch.del_phone,ch.client_del_addr,ch.off_phone,ch.client_off_addr,ch.state_serialnumber_fk,ch.country_serialnumber_fk,ch.cust_type,ch.client_name,ch.client_id,ch.serialnumber,con.country_name
		from client_header as ch LEFT JOIN country_header as con ON con.serialnumber=ch.country_serialnumber_fk where ch.activestatus=1") or die(mysql_error());
        $ret = array();
        while ($row = mysql_fetch_object($result)) {
            $tmp = new VoClientMaster();
            $tmp->serialnumber = (int) $row->serialnumber;
            $tmp->client_id = $row->client_id;
            $tmp->client_name = $row->client_name;
            $tmp->cust_type = $row->cust_type;
            $tmp->country_serialnumber_fk = (int) $row->country_serialnumber_fk;
            $tmp->state_serialnumber_fk = (int) $row->state_serialnumber_fk;
			$tmp->country_name = $row->country_name;
            $tmp->client_off_addr = $row->client_off_addr;
            $tmp->off_phone = $row->off_phone;
            $tmp->client_del_addr = $row->client_del_addr;
            $tmp->del_phone = $row->del_phone;
            $tmp->client_fax = $row->client_fax;
            $tmp->client_email = $row->client_email;
            $tmp->client_url = $row->client_url;
            $tmp->client_contact_person = $row->client_contact_person;
            $tmp->client_contact_phone = $row->client_contact_phone;
            $tmp->client_contact_email = $row->client_contact_email;
            $tmp->client_remarks = $row->client_remarks;
            $tmp->client_regno1 = $row->client_regno1;
            $tmp->client_regno2 = $row->client_regno2;
            $tmp->client_pricecategory = $row->client_pricecategory;
            $tmp->cre_userid = (int) $row->cre_userid;
            $tmp->cre_datetime = $row->cre_datetime;
            $tmp->activestatus = (boolean) $row->activestatus;
            $tmp->change_client_id = false;
            $ret[] = $tmp;
        }
        mysql_free_result($result);
        return $ret;
    }

    public function editClient($VObject) {
        if ($VObject->change_client_id == true) {
            $client_id = "";

            if ($VObject->cust_type == "CUSTOMER") {
                $client_id = $this->generateClientCustomerCode();
                mysql_query("insert into client_customer_code(customer_code) values ('$client_id')") or die(mysql_error());
            } else if ($VObject->cust_type == "SUPPLIER") {
                $client_id = $this->generateClientSupplierCode();
                mysql_query("insert into client_supplier_code(supplier_code) values ('$client_id')") or die(mysql_error());
            } else if ($VObject->cust_type == "BOTH") {
                $client_id = $this->generateClientBothCode();
                mysql_query("insert into client_both_code(both_code) values ('$client_id')") or die(mysql_error());
            } else if ($VObject->cust_type == "UN-CATEGORIZED") {
                $client_id = $this->generateClientUncategorizedCode();
                mysql_query("insert into client_uncategorized_code(uncategorized_code) values ('$client_id')") or die(mysql_error());
            }

            $query = "update client_header set client_id='$client_id',client_name='$VObject->client_name',cust_type='$VObject->cust_type',country_serialnumber_fk='$VObject->country_serialnumber_fk',state_serialnumber_fk='$VObject->state_serialnumber_fk',client_off_addr='$VObject->client_off_addr',off_phone='$VObject->off_phone',client_del_addr='$VObject->client_del_addr',del_phone='$VObject->del_phone',client_fax='$VObject->client_fax',client_email='$VObject->client_email',client_url='$VObject->client_url',client_contact_person='$VObject->client_contact_person',client_contact_phone='$VObject->client_contact_phone',client_contact_email='$VObject->client_contact_email',client_remarks='$VObject->client_remarks',client_regno1='$VObject->client_regno1',client_regno2='$VObject->client_regno2',client_pricecategory='$VObject->client_pricecategory',cre_userid='$VObject->cre_userid',cre_datetime='$VObject->cre_datetime' where serialnumber=" . $VObject->serialnumber;
            $s = mysql_query($query) or die(mysql_error());
        } else {
            $query = "update client_header set client_name='$VObject->client_name',cust_type='$VObject->cust_type',country_serialnumber_fk='$VObject->country_serialnumber_fk',state_serialnumber_fk='$VObject->state_serialnumber_fk',client_off_addr='$VObject->client_off_addr',off_phone='$VObject->off_phone',client_del_addr='$VObject->client_del_addr',del_phone='$VObject->del_phone',client_fax='$VObject->client_fax',client_email='$VObject->client_email',client_url='$VObject->client_url',client_contact_person='$VObject->client_contact_person',client_contact_phone='$VObject->client_contact_phone',client_contact_email='$VObject->client_contact_email',client_remarks='$VObject->client_remarks',client_regno1='$VObject->client_regno1',client_regno2='$VObject->client_regno2',client_pricecategory='$VObject->client_pricecategory' where serialnumber=" . $VObject->serialnumber;
            $s = mysql_query($query) or die(mysql_error());
        }
        if ($s == 1) {
            mysql_query("COMMIT");
        } else {
            mysql_query("ROLLBACK");
        }
        return $this->getAllClients();
    }

    public function deleteClient($serialnumber) {
        $query = "update client_header set activestatus=0 where serialnumber=" . $serialnumber;
        $s = mysql_query($query) or die(mysql_error());
        if ($s == 1) {
            mysql_query("COMMIT");
        } else {
            mysql_query("ROLLBACK");
        }
        return 'true';
    }

}

class VoFilteredClient {
    var $serialnumber;
    var $client_name;
    var $client_email;
    var $client_contact_email;
    var $isSelected;
    var $hilight;
	var $client_contact_phone;
	var $client_contact_person;
}

class VoClientMaster {

    var $serialnumber;
    var $client_id;
    var $change_client_id;
    var $client_name;
    var $cust_type;
    var $country_serialnumber_fk;
    var $state_serialnumber_fk;
    var $client_off_addr;
    var $off_phone;
    var $client_del_addr;
    var $del_phone;
    var $client_fax;
    var $client_email;
    var $client_url;
    var $client_contact_person;
    var $client_contact_phone;
    var $client_contact_email;
    var $client_remarks;
    var $client_regno1;
    var $client_regno2;
    var $client_pricecategory;
    var $cre_userid;
    var $cre_datetime;
    var $activestatus;

}

?>